package vip.zhenzicheng.algorithm.leetcode.array;

import java.util.Arrays;

/**
 * <a href="https://leetcode.cn/problems/rotate-image/">旋转图像 [中等]</a>
 *
 * @author zhenzicheng
 * @date 2022-06-17 14:05
 */
public class RotateImage_48 {

  public static void main(String[] args) {
    RotateImage_48 test = new RotateImage_48();
    int[][] matrix = new int[][]{{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
    test.rotate(matrix);
    System.out.println(Arrays.toString(matrix));
  }

  public void rotate(int[][] matrix) {
    int x, y;
    int N = matrix.length - 1;
    /*先沿左下到右上的对角线对称交换*/
    for (x = 0; x <= N; x++) {
      for (y = 0; y <= N - x; y++) {
        int temp = matrix[x][y];
        matrix[x][y] = matrix[N - y][N - x];
        matrix[N - y][N - x] = temp;
      }
    }

    /*再上下沿中间对角线对称交换*/
    for (x = 0; x <= N / 2; x++) {
      for (y = 0; y <= N; y++) {
        int temp = matrix[x][y];
        matrix[x][y] = matrix[N - x][y];
        matrix[N - x][y] = temp;
      }
    }
  }
}
